package iit.projects.etaxi.view.bean;

import iit.projects.etaxi.model.dto.CitySCDO;
import iit.projects.etaxi.model.entity.City;
import iit.projects.etaxi.model.service.CityService;
import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.JSFUtils;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icesoft.faces.component.ext.HtmlInputText;

public class CityBackingBean {
	
	final Logger log = LoggerFactory.getLogger(CityBackingBean.class);
	
	//private CitySCDO citySCDO;
	
	private City city;
	
	//private City initialCity;
	
	private List<City> cities;
	
	private boolean editMode;
	
	private HtmlInputText compCode;
	
	private HtmlInputText compName;
	
	private ServiceLocator serviceLocator;

	private CityService cityService;
	
	private String selectedCityCodeForDelete;
	
	public CityBackingBean() {
	}
	
	@javax.annotation.PostConstruct
	public void init(){
		//citySCDO = new CitySCDO();
		city = new City();
		//initialCity = new City();
		cityService = serviceLocator.getCityService();
	}
	
	public void saveCity(ActionEvent event){
		
		if(!valid()){
			return;
		}
		try {
			if(!editMode){
				//City city = new City(this.citySCDO.getCode(), this.citySCDO.getName());
				City city = cityService.saveCity(this.city);
				if(this.cities == null){
					this.cities = new ArrayList<City>();
				}
				this.cities.add(city);
			}
			else{
				City city = cityService.updateCity(this.city);
//				int indexOfUpdatedCity = this.cities.indexOf(city);
//				this.cities.remove(indexOfUpdatedCity);
//				this.cities.add(indexOfUpdatedCity, city);
				this.cities.set(this.cities.indexOf(city), city);
			}
			JSFUtils.addFacesInfoMessage("city.successfully.saved");
			clear(event);
		} catch (Throwable e) {
//			this.city = new City(this.initialCity);
			clear(event);
			return;
		}
	}
	
	public void searchCities(ActionEvent event){
		
		CitySCDO citySCDO = new CitySCDO();
		citySCDO.setCode(this.city.getCode());
		citySCDO.setName(this.city.getName());
		
		this.cities = cityService.findCities(citySCDO);
		if(this.cities == null || this.cities.isEmpty()){
			JSFUtils.addFacesInfoMessage("data.notfound");
			return;
		}
	}
	
	public void deleteCity(ActionEvent event){
		
		for(City city : this.cities){
			if(city.getCode().equals(selectedCityCodeForDelete)){
				
				try{
					cityService.deleteCity(city);
					this.cities.remove(city);
				}catch(Throwable e){
					return;
				}
				break;
			}
		}
	}
	
	public void confirmDelete(ActionEvent event){
		ConfirmationPopupBackingBean confirmBean = 
			(ConfirmationPopupBackingBean)JSFUtils.getManagedBeanValue("confirmationPopup");
		confirmBean.setMessage(JSFUtils.getResourcesProperty("delete.city.confirmation"));
		confirmBean.setBackingBeanAlias("cityBean");
		confirmBean.setMethodName("deleteCity");
		confirmBean.setShowPopup(true);
		
		selectedCityCodeForDelete = (String)JSFUtils.getRequestParameter("cityCode");
	}
	
	public void clear(ActionEvent event){
		this.editMode = false;
		//this.citySCDO = new CitySCDO();
		this.city = new City();
		
		compCode.resetValue();
		compName.resetValue();
	}
	
	public void loadCity(ActionEvent event){
		
		String selectCityCode = (String)JSFUtils.getRequestParameter("cityCode");
		log.debug("Found City : " + selectCityCode);
		for(City city : this.cities){
			if(city.getCode().equals(selectCityCode)){
//				this.citySCDO.setCode(city.getCode());
//				this.citySCDO.setName(city.getName());
				
				this.city = new City(city);
				//this.initialCity = city;
				
				compCode.resetValue();
				compName.resetValue();
				break;
			}
		}
		
		this.editMode = true;
	}
	
	private boolean valid(){
		
		boolean valid = true;
		if(this.city.getCode() == null || this.city.getCode().isEmpty()){
			JSFUtils.addFacesErrorMessage(compCode, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		if(this.city.getName() == null || this.city.getName().isEmpty()){
			JSFUtils.addFacesErrorMessage(compName, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		return valid;
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

//	public CitySCDO getCitySCDO() {
//		return citySCDO;
//	}
//
//	public void setCitySCDO(CitySCDO citySCDO) {
//		this.citySCDO = citySCDO;
//	}

	public HtmlInputText getCompCode() {
		return compCode;
	}

	public void setCompCode(HtmlInputText compCode) {
		this.compCode = compCode;
	}

	public HtmlInputText getCompName() {
		return compName;
	}

	public void setCompName(HtmlInputText compName) {
		this.compName = compName;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public List<City> getCities() {
		return cities;
	}

	public void setCities(List<City> cities) {
		this.cities = cities;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

}
